﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
using System;

class MagicMenuHelper
{

    /// <summary>
    /// 当前选中的资源/文件夹的路径
    /// </summary>
    public static string SelectObjectPath=> AssetDatabase.GetAssetPath(Selection.activeObject);
  

    //创建脚本
    public static void CreatScript(string scriptName,string path)
    {
        string nameSpece = path.Replace("Assets/", "").Replace("/", ".").Replace(" ", "_")+"_";
      string  thisScriptStr = scriptStr.Replace("%NAMESPACE%", nameSpece);
        thisScriptStr = thisScriptStr.Replace("%CLASSNAME%", scriptName);
        string scriptPath = (Path.Combine(Path.GetFullPath(path), scriptName + ".cs"));
        File.WriteAllText(scriptPath, thisScriptStr, Encoding.UTF8);
    }
    //创建中心点在地面的物体
    public static GameObject CreatePrimitive(PrimitiveType primitiveType, string name, float yOffset)
    {
        GameObject parent = new GameObject(name);
        var graphic = GameObject.CreatePrimitive(primitiveType);
        graphic.transform.SetParent(parent.transform);
        graphic.transform.localPosition = new Vector3(0, yOffset, 0);
        if (Selection.activeGameObject != null)
        {
            parent.transform.position = Selection.activeGameObject.transform.position;
        }
        Selection.activeGameObject = parent;
        return parent;
    }


    #region 

    static string scriptStr =
@"using System.Collections;
using UnityEngine;

namespace %NAMESPACE%
{
    public class %CLASSNAME% : MonoBehaviour
    {
      
    }
}";

    internal static void CopyAllSelectedPath()
    {
        //拿到当前选中的游戏物体
        var goArr = Selection.gameObjects;
        //sb用来拼接字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < goArr.Length; i++)
        {
            Transform tf = goArr[i].transform;
            string path= goArr[i].name;
            while (tf.parent!=null)
            {
                tf = tf.parent;
                path = tf.name + "/" + path;
            }
            sb.Append(path);
            if (i != goArr.Length - 1)
            {
                sb.Append("\n");
            }
        }
        string str = sb.ToString();
        //复制到剪切板
        TextEditor te = new TextEditor();
        te.text = str;
        te.SelectAll();
        te.Copy();
    }

    //复制所有的选中的物体的名称
    internal static void CopyAllSelectedName()
    {
        var goArr = Selection.gameObjects;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < goArr.Length; i++)
        {
            sb.Append(goArr[i].name);
            if (i!=goArr.Length-1)
            {
                sb.Append("\n");
            }
        }
        string str = sb.ToString();
        //复制到剪切板
        TextEditor te = new TextEditor();
        te.text = str;
        te.SelectAll();
        te.Copy();
    }
    #endregion
}





